package GUI.view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;

import client.domainModels.NodeImpl.LogFile;

/**
*
* @author Wei WANG
*/
public class HomePage extends JPanel implements ActionListener, ItemListener {

	JTextArea output;
	JScrollPane scrollPane;
	String newline = "\n";
	JFileChooser fc;
	JMenuBar menuBar;
	JMenu menu, submenu;
	JMenuItem menuItemLoad;
	JMenuItem menuItemCreate;
	JRadioButtonMenuItem rbMenuItem;
	JCheckBoxMenuItem cbMenuItem;
	FileManager filemanager;
	
	public HomePage() {
		initPanel();
	}

	private void initPanel() {
		this.setPreferredSize(new Dimension(600,700));
		//this.setJMenuBar(createMenuBar());
		//this.setContentPane(createContentPane());
		this.setLayout(new BorderLayout());
		this.add(createContentPane(),BorderLayout.CENTER);
	}

	public Container createContentPane() {
		final JPanel contentPane = new JPanel(new BorderLayout());
		contentPane.setOpaque(true);
		return contentPane;
	}

	
	public JMenuBar createMenuBar() {
		fc = new JFileChooser();

		menuBar = new JMenuBar();
		menu = new JMenu("File");
		menu.setMnemonic(KeyEvent.VK_F);
		menuBar.add(menu);

		menuItemLoad = new JMenuItem("Load XML File", KeyEvent.VK_L);
		menuItemLoad.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1,
				ActionEvent.ALT_MASK));
		menuItemLoad.addActionListener(this);
		menu.add(menuItemLoad);
		
		menuItemCreate = new JMenuItem("Create XML File", KeyEvent.VK_C);
		/*menuItemCreate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1,
				ActionEvent.ALT_MASK));*/
		menuItemCreate.addActionListener(this);
		
		menu.add(menuItemCreate);
		menuBar.add(menu);
		return menuBar;
	}
	
	
	@Override
	public void actionPerformed(final ActionEvent e) {
		// TODO Auto-generated method stub

		if (e.getSource() == menuItemLoad) {
			final int returnVal = fc.showOpenDialog(null);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				final File file = fc.getSelectedFile();
				final LogFile log = LogToMemory.logToMemoryAndDisplay(file);
				final JTable table = MultiLineTableConstructor.createLogTable(log);
				final JScrollPane scrollPanel = new JScrollPane(table);
				final JPanel panel = new JPanel();
				panel.setLayout(new BorderLayout());
				panel.add(scrollPanel, BorderLayout.CENTER);
				//this.getContentPane().removeAll();
				//this.getContentPane().add(panel);
				this.removeAll();
				this.add(panel);
				
				this.validate();
				//this.setContentPane(panel);
			} else{
				
			}

		}else if(e.getSource() == menuItemCreate){
			final FileManager fileManager = new FileManager();
			//this.getContentPane().removeAll();
			//this.getContentPane().add(fileManager);
			
			this.removeAll();
			this.add(fileManager);
			//this.validate();
			this.updateUI();
		}

	}

	@Override
	public void itemStateChanged(final ItemEvent e) {
		final JMenuItem source = (JMenuItem) (e.getSource());
		final String s = "Item event detected."
				+ newline
				+ "    Event source: "
				+ source.getText()
				+ " (an instance of "
				+ getClassName(source)
				+ ")"
				+ newline
				+ "    New state: "
				+ ((e.getStateChange() == ItemEvent.SELECTED) ? "selected"
						: "unselected");
		output.append(s + newline);
		output.setCaretPosition(output.getDocument().getLength());
	}
	protected String getClassName(final Object o) {
		final String classString = o.getClass().getName();
		final int dotIndex = classString.lastIndexOf(".");
		return classString.substring(dotIndex + 1);
	}

	
	//////////////////// For test /////////////////////////////
	/*private static void createAndShowGUI() {
		// Create and set up the window.
		final JFrame frame = new HomePage();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		
		// Show the window.
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(final String[] args) {
		// Schedule a job for the event-dispatching thread:
		// creating and showing this application's GUI.
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				createAndShowGUI();
			}
		});
	}*/
  //////////////////////////////////////////////////////////////

}
